#include <iostream>
#define STACK_MAX 10
typedef int elemtype;

class my_stacklist{
    elemtype data;//顺序栈
    elemtype* top;
    elemtype* base;
    int maxsize;
    bool initstack(){
        base=top=new elemtype[STACK_MAX];
        if(base== nullptr){
            return false;
        }else{
            return true;
        }
    }
public:
    my_stacklist(){
      initstack();
    }
    int getsize(){
        return top-base;
    }
    bool isempty() {
        if (top == base)
            return true;
    }

    void push(elemtype e){
        if((top-base)==STACK_MAX){
            std::cout<<"栈满"<<std::endl;
        }
        *top++=e;
    }
    void pop(){
        if(top==base){
            std::cout<<"栈空"<<std::endl;
        }
        --top;
    }
    elemtype gettop(){
        if(top==base){
            std::cout<<"栈空"<<std::endl;
            return 9999;
        }else{
            return *(top-1);
        }
    }
    void clear(){
        top=base;
    }
    ~my_stacklist(){
        if(base!=NULL)
            delete[] base;
    }
};
struct stack_node{
    elemtype data;
    stack_node *next;
};
class my_stacknode{
    stack_node* headnode;
public:
    my_stacknode(){
        headnode=NULL;
    }
    void push(elemtype e){
        if(headnode==NULL){
            headnode= new stack_node;
            headnode->data=e;
            headnode->next=NULL;
        }else{
            stack_node* p= new stack_node;
            p->next=headnode;
            p->data=e;
            headnode=p;
        }
    }
    bool isemtpy(){
       return headnode==NULL;

    }
    void pop(){
        if(headnode==NULL){
            std::cout<<"栈空"<<std::endl;
        } else{
            stack_node* p=headnode;
            headnode=headnode->next;
            delete p;
        }
    }
    elemtype gettop(){
        if(isemtpy())
            return 9999;
        return headnode->data;
    }
    ~my_stacknode(){
        stack_node* p;
        while(headnode!=NULL){
            p=headnode;
            headnode=headnode->next;
            delete p;
        }
    }
};

int main() {
    int array[5]={0,1,2,3,4};
    my_stacknode L;
    for(int i=0;i<7;i++){
        L.push(i+1);
    }
    for(int i=0;i<7;i++){
        std::cout<<L.gettop()<<std::endl;
        L.pop();
    }

    my_stacklist s;
    for(int i=0;i<5;i++){
        s.push(array[i]);
    }
    std::cout<<"stack size="<<s.getsize()<<std::endl;
    for(int i=0;i<5;i++){
        std::cout<<"top第"<<i+1<<"个元素为："<<s.gettop()<<std::endl;
        s.pop();
    }
    return 0;
}
